<?php

namespace App\Http\Requests\Admin\Goods;

use App\Exceptions\ResponseException;
use App\Models\GoodsCategory;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class GoodsCreateRequest extends FormRequest
{
	/**
	 * 重新验证失败的方法，让其返回json的错误信息
	 * User: ihome
	 * @param \Illuminate\Contracts\Validation\Validator $validator
	 */
	public function failedValidation($validator)
	{
		$message = $validator->errors()->first();

		throw new ResponseException(422,$message);
	}

    public function rules()
    {
        return [
            'category_id'=>[
            	'required',
				'exists:goods_category,id',
				'integer'
			],
			'brand_id'=>'required|exists:goods_brand,id|integer',
			'goods_name'=>[
				'required',
				Rule::unique('goods'),
				'string',
				'max:50',
				'min:2'
			],
			'sort'=>'required|integer|max:100|min:0',
			'status'=>[
				'required',
				'integer',
				Rule::in(['0','1'])
			],
			'description'=>'required|string|max:10000|min:1',
			'type_id'=>'required|exists:goods_type,id'
        ];
    }

	public function attributes()
    {
		return [
			'category_id'=>'商品分类',
			'brand_id'=>'品牌',
			'goods_name'=>'商品名称',
			'sort'=>'商品排序',
			'status'=>'商品状态',
			'description'=>'商品描述',
			'type_id'=>'商品类型'
		];
    }
}
